using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LocalizerManager : StuffObject<LocalizerManager>
{
    [ShowInInspector]
    public string Language => LanguageManager.Stuff.Language;

    [ShowInInspector]
    public int LocalizerCount => this.localizerSet.Count;


    HashSet<Localizer> localizerSet = new HashSet<Localizer>();

    private void Awake()
    {
        LanguageManager.Stuff.LanguageChanged += this.OnLanguageChanged;
    }

    void OnLanguageChanged(string language)
    {
        this.RefreshAllLocalizer();
    }

    bool isDataLoaded;
    public void SetDataLoaded()
    {
        isDataLoaded = true;
        this.RefreshAllLocalizer();
    }

    public void RefreshAllLocalizer()
    {
        if(!isDataLoaded)
        {
            return;
        }
        foreach(var localizer in localizerSet) 
        {
            localizer.Language = LanguageManager.Stuff.Language;
        }
    }

    public void Register(Localizer localizer)
    {
        localizerSet.Add(localizer);
        if(this.isDataLoaded)
        {
            localizer.Language = this.Language;
        }
    }

    public void Unregiser(Localizer localizer)
    {
        localizerSet.Remove(localizer);
    }
}
